{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 对 df['Age'] 中缺失的数值用平均年龄进行填充\n",
    "# df['Age'].fillna(df['Age'].mean(), inplace=True)\n",
    "# 对 df['Age'] 中缺失的数值用最高频的数据进行填充\n",
    "age_maxf = df['Age'].value_counts().index[0]\n",
    "df['Age'].fillna(age_maxf, inplace=True)\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 删除全空的行\n",
    "df.dropna(how='all',inplace=True)\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 将磅（lbs）转化为千克（kgs）\n",
    "# 获取 weight 数据列中单位为 lbs 的数据\n",
    "rows_with_lbs = df['weight'].str.contains('lbs').fillna(False)\n",
    "print (df[rows_with_lbs])\n",
    "# 将 lbs转换为 kgs, 2.2lbs=1kgs\n",
    "for i,lbs_row in df[rows_with_lbs].iterrows():\n",
    "    # 截取从头开始到倒数第三个字符之前，即去掉lbs。\n",
    "    weight = int(float(lbs_row['weight'][:-3])/2.2)\n",
    "    df.at[i,'weight'] = '{}kgs'.format(weight)\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 删除非 ASCII 字符\n",
    "df['name'].replace({r'[^\\x00-\\x7F]+':''}, regex=True, inplace=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 切分名字，删除源数据列\n",
    "df[['first_name','last_name']] = df['name'].str.split(expand=True)\n",
    "df.drop('name', axis=1, inplace=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"E:/临时数据/accountMessage.xlsx\")\n",
    "print(df)\n",
    "# 删除重复数据行\n",
    "df.drop_duplicates(['name'],inplace=True)\n",
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
